using System;
using System.Xml;
using System.Collections.Generic;

using Microsoft.SharePoint;

using RealDolmen.Helpers;

namespace RealDolmen.SharePoint.Helpers
{
    public class SPFieldHelper
    {
        #region Fields
        private SPField field;
        private Dictionary<string, string> props; 
        #endregion // Fields

        #region Constructor
        public SPFieldHelper(SPField field)
        {
            props = new Dictionary<string, string>();
            this.field = field;
        }
        #endregion // Constructor

        #region GetAttribute
        public string GetAttributeValue(string attrName)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(field.SchemaXml);

            return XmlHelper.GetAttribute(xmlDoc.FirstChild, attrName);
        }

        public bool GetAttributeValueAsBool(string attrName)
        {
            string value = GetAttributeValue(attrName);

            if (string.IsNullOrEmpty(value) || ((value.ToUpper() != "TRUE") && !(value == "-1")))
            {
                return false;
            }
            return true;
        } 
        #endregion // GetAttribute

        #region SetAttribute
        public void SetAttributeValue(string attrName, string attrValue, bool updateNow)
        {
            props[attrName] = attrValue;

            if (updateNow)
                Update();
        }

        public void SetAttributeValueAsBool(string attrName, bool bValue, bool updateNow)
        {
            SetAttributeValue(attrName, bValue ? "TRUE" : "FALSE", updateNow);
        } 
        #endregion // SetAttribute

        #region Update
	    public void Update()
        {
            if (props.Count > 0)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(field.SchemaXml);
                XmlAttributeCollection fieldAttributes = xmlDoc.FirstChild.Attributes;


                bool hasId = false;

                try
                {
                    hasId = !(field.Id == Guid.Empty);
                }
                catch { }

                if (! hasId)
                {
                    foreach (KeyValuePair<string, string> prop in props)
                    {
                        field.SetCustomProperty(prop.Key, prop.Value);
                    }
                }
                else
                {
                    foreach (KeyValuePair<string, string> prop in props)
                    {
                        SetField(prop.Key, prop.Value, xmlDoc, fieldAttributes);
                    }

                    try
                    {
                        field.SchemaXml = xmlDoc.OuterXml;
                        field.Update();
                    }
                    catch { }
                }
                props.Clear();
            }
        }

        private static void SetField(string attrName, string attrValue, XmlDocument xmlDoc, XmlAttributeCollection fieldAttributes)
        {
            XmlNode node = fieldAttributes.GetNamedItem(attrName);

            if (node == null)
            {
                XmlAttribute attribute = xmlDoc.CreateAttribute(attrName);
                attribute.Value = attrValue;
                fieldAttributes.SetNamedItem(attribute);
            }
            else
                node.Value = attrValue;
        }
    	#endregion // Update
    }
}
